// 工具箱
import ToolsFactory from './toolsFactory'

// 根据数据绘制剖面信息
export default class DrawProfile {
    constructor() {
        this.length = 0;
        this.index = 0;
        this.dataList = [];
        this.DataSource = null;
        this.toolsList = new ToolsFactory()
        this.myEntityCollectionList = []
    }
    delDataSource() {
        this.DataSource && viewer.dataSources.remove(this.DataSource);
    }
    init(data, cb) {
        this.index = 0;
        this.dataList = [];
        this.dataList = data;
        this.defaultSet();
        // this.DataSource = null;
        // viewer.dataSources.removeAll();
        this.DataSource = this.DataSource || new Cesium.CustomDataSource('profileEntities');
        if (this.dataList.length > 0) {
            this.drawSingleEntity(this.dataList[this.index])
        }
        viewer.dataSources.add(this.DataSource);


    }
    SourceshowHide(t) {
        // const poOBjArrsource = viewer.dataSources.getByName('profileEntities') || [];
        // poOBjArrsource.forEach(item => {
        //     item.show = t;
        // })
        if (viewer.dataSources.getByName("profileEntities")[0]) {
            viewer.dataSources.getByName("profileEntities")[0].entities.values.forEach(en => {
                en.show = t
            })
        }


    }
    drawSingleEntity(target) {
        const pos = [];
        const posti = target.coordinates
        const promLis = [];
        for (let i = 0; i < posti.length; i += 2) {
            promLis.push(this.toolsList.getCoordinatesByLonLat(target.coordinates[i], target.coordinates[i + 1]));
            pos.push(Cesium.Cartesian3.fromDegrees(target.coordinates[i], target.coordinates[i + 1]));
        }

        this.DataSource.entities.add({
            id: 'poumian' + target.eventid,
            name: "poumian",
            polyline: {
                positions: pos,
                clampToGround: true,
                width: 4,
                material: new Cesium.PolylineDashMaterialProperty({
                    color: Cesium.Color.ORANGE,
                    gapColor: Cesium.Color.ORANGE.withAlpha(0.5),
                    dashLength: 4.0,
                }),
            },
            position: Cesium.Cartesian3.fromDegrees(target.coordinates[0], target.coordinates[1], 0),
            label: {
                text: target.name,
                color: Cesium.Color.fromCssColorString('#fff'),
                font: 'normal 14px MicroSoft YaHei',
                showBackground: true,
                outlineColor: new Cesium.Color(0.0, 0.0, 0.0, 1.0),
                outlineWidth: 2.0,
                heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
                scale: 1,
                pixelOffset: new Cesium.Cartesian2(0.0, 0.0),
                pixelOffsetScaleByDistance: new Cesium.NearFarScalar(1.5e2, 0.0, 8.0e6, 1.0),//根据距照相机的距离指定广告牌的像素偏移
                distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0.0, 30000.0),
            },
        });

        if (this.index <= this.dataList.length - 1) {
            this.index++;
            this.dataList[this.index] && this.drawSingleEntity(this.dataList[this.index])
        } else {

            viewer.flyTo(this.DataSource);
        }





        // this.DataSource.entities.add({
        //     id: 'poumian' + target.eventid,
        //     name: "poumian",
        //     polyline: {
        //         positions: pos,
        //         width: 6.0,
        //         clampToGround: true, //折线固定在地面
        //         material: new Cesium.PolylineOutlineMaterialProperty({
        //             color: Cesium.Color.ORANGE,
        //             outlineWidth: 1,
        //             outlineColor: Cesium.Color.BLACK,
        //         }),
        //     },
        // });




        // Promise.all(promLis).then((res) => {
        //     let myEntityCollection = new Cesium.CustomDataSource('poumianDian' + target.eventid);
        //     this.myEntityCollectionList.push(target.eventid)
        //     res.forEach((item, index) => {
        //         if (index % 10 === 0) {
        //             myEntityCollection.entities.add(
        //                 {
        //                     id: 'poumian' + index + 'poumianDian' + target.eventid,
        //                     name: "poumianDian",
        //                     position: Cesium.Cartesian3.fromDegrees(item.lon, item.lat, item.height),
        //                     point: {
        //                         color: Cesium.Color.ORANGE,
        //                         pixelSize: 6,
        //                         heightReference: Cesium.HeightReference.CLAMP_TO_GROUND
        //                     }
        //                 }
        //             );
        //         }

        //         // this.DataSource.entities.add({
        //         //     position: Cesium.Cartesian3.fromDegrees(item.lon, item.lat, item.height),
        //         //     point: {
        //         //         color: Cesium.Color.ORANGE,
        //         //         pixelSize: 5,
        //         //         heightReference: Cesium.HeightReference.CLAMP_TO_GROUND
        //         //     }
        //         // })
        //     })

        //     myEntityCollection.entities.add({
        //         position: Cesium.Cartesian3.fromDegrees(target.coordinates[0], target.coordinates[1], 0),
        //         label: {
        //             text: target.name,
        //             color: Cesium.Color.fromCssColorString('#fff'),
        //             font: 'normal 14px MicroSoft YaHei',
        //             showBackground: true,
        //             outlineColor: new Cesium.Color(0.0, 0.0, 0.0, 1.0),
        //             outlineWidth: 2.0,
        //             heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
        //             scale: 1,
        //             pixelOffset: new Cesium.Cartesian2(0.0, 0.0),
        //             pixelOffsetScaleByDistance: new Cesium.NearFarScalar(1.5e2, 0.0, 8.0e6, 1.0),//根据距照相机的距离指定广告牌的像素偏移
        //             distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0.0, 30000.0),
        //         },
        //     })
        //     viewer.dataSources.add(myEntityCollection);
        //     if (this.index <= this.dataList.length - 1) {
        //         this.index++;
        //         this.dataList[this.index] && this.drawSingleEntity(this.dataList[this.index])
        //     }

        // });
    }
    defaultSet() {
        // viewer.scene.globe.depthTestAgainstTerrain = false;
        // viewer.scene.fxaa = false
        // viewer.scene.postProcessStages.fxaa.enabled = false;
        // var supportsImageRenderingPixelated = viewer.cesiumWidget._supportsImageRenderingPixelated;
        // if (supportsImageRenderingPixelated) {
        //     var vtxf_dpr = window.devicePixelRatio;
        //     while (vtxf_dpr >= 2.0) { vtxf_dpr /= 2.0; }
        //     viewer.resolutionScale = vtxf_dpr;
        // }
    }
    changeProfileStyle(id) {
        // 设置点的颜色
        // const poOBjArr = viewer.dataSources.getByName("poumianDian" + id) || [];
        // if (poOBjArr.length) {
        //     poOBjArr.forEach((item) => {
        //         if (item.entities.values && item.entities.values.length) {
        //             item.entities.values.forEach((itemEnt) => {
        //                 itemEnt.point && (itemEnt.point.color._value = new Cesium.Color(
        //                     220 / 255.0,
        //                     20 / 255.0,
        //                     0 / 255.0,
        //                     1
        //                 ));
        //             });
        //         }
        //     });
        // }
        let entBox = null;
        const poOBjArrsource = viewer.dataSources.getByName("profileEntities") || [];
        if (poOBjArrsource.length) {
            entBox = poOBjArrsource[0].entities.getById(
                "poumian" + id
            );
            // 设置线的颜色
            entBox.polyline.material = new Cesium.PolylineDashMaterialProperty({
                color: Cesium.Color.ORANGERED,
                gapColor: Cesium.Color.ORANGERED.withAlpha(0.5),
                dashLength: 4.0,
            });
            viewer.flyTo(entBox);
        }

    }
    backOriginalStyle(id) {
        // const poOBjArrsource = viewer.dataSources.getByName("profileEntities") || [];
        let entBox = null;
        const poOBjArrsource = viewer.dataSources.getByName("profileEntities") || [];
        if (poOBjArrsource.length) {
            entBox = poOBjArrsource[0].entities.getById(
                "poumian" + id
            );
            // 设置线的颜色
            entBox.polyline.material = new Cesium.PolylineDashMaterialProperty({
                color: Cesium.Color.ORANGE,
                gapColor: Cesium.Color.ORANGE.withAlpha(0.5),
                dashLength: 4.0,
            });
        }



        // if (poOBjArrsource.length) {
        //     const entBox = poOBjArrsource[0].entities.getById(
        //         "poumian" + id
        //     );
        //     // 设置线的颜色
        //     entBox.polyline.material = new Cesium.PolylineOutlineMaterialProperty({
        //         color: Cesium.Color.ORANGE,
        //         outlineWidth: 1,
        //         outlineColor: Cesium.Color.BLACK,
        //     });
        // }
        // 设置点的颜色
        // const poOBjArr =
        //     viewer.dataSources.getByName("poumianDian" + id) ||
        //     [];
        // if (poOBjArr.length) {
        //     poOBjArr.forEach((item) => {
        //         if (item.entities.values && item.entities.values.length) {
        //             item.entities.values.forEach((itemEnt) => {
        //                 itemEnt.point && (itemEnt.point.color._value = new Cesium.Color(
        //                     255 / 255.0,
        //                     165 / 255.0,
        //                     0 / 255.0,
        //                     1
        //                 ));
        //             });
        //         }
        //     });
        // }

    }
}